<template>
  <div class="box">
    <el-row>
      <el-col :span="24">
        <div class="header">
          <el-form ref="form" :model="form" label-width="100px">
            <el-row>
              <el-col :span="24" :md="12">
                <div>
                  <el-form-item label="查询时间">
                    <el-date-picker
                      v-model="form.value1"
                      style="width: 100%"
                      type="datetimerange"
                      range-separator="至"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                      clearable
                    />
                  </el-form-item>
                </div>
              </el-col>
              <el-col :span="12" :md="6">
                <el-form-item label="网点名称">
                  <networkSelect
                    :selected-network="form.region"
                    @select="handleSelect"
                  />
                </el-form-item>
              </el-col>
              <el-col v-show="activeType == 1" :span="12" :md="6">
                <el-form-item label="统计维度">
                  <el-select
                    v-model="form.collectType"
                    clearable
                    placeholder="请选择统计维度"
                    style="width: 100%"
                  >
                    <el-option label="按网点统计" value="1" />
                    <el-option label="按承包区统计" value="2" />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-show="activeType == 2" :span="12" :md="6">
                <el-form-item label="工单渠道">
                  <el-select
                    v-model="form.sheetChannelId"
                    clearable
                    placeholder="请选择工单渠道"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in sheetChannelList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-show="activeType == 3" :span="12" :md="6">
                <el-form-item label="工单类型">
                  <el-select
                    v-model="form.sheetTypeId"
                    clearable
                    placeholder="请选择工单类型"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in sheetTypeList"
                      :key="item.id"
                      :label="item.name"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col v-show="activeType == 4" :span="12" :md="6">
                <el-form-item label="员工名称">
                  <el-select
                    v-model="form.processorId"
                    clearable
                    placeholder="请选择统员工名称"
                    style="width: 100%"
                  >
                    <el-option
                      v-for="item in solicitorList"
                      :key="item.id"
                      :label="item.solicitor"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="24" class="block">
                <el-button type="primary" @click="onSubmit">查询</el-button>
                <el-button @click="resettingTap">重置</el-button>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-col>
    </el-row>

    <div class="mytabs">
      <el-tabs v-model="activeType" @tab-click="handleClick">
        <el-tab-pane label="网点维度" :name="'1'" />
        <el-tab-pane label="渠道维度" :name="'2'" />
        <el-tab-pane label="类型维度" :name="'3'" />
        <el-tab-pane label="处理人维度" :name="'4'" />
        <el-row type="flex" justify="space-between">
          <el-col :span="24">
            <el-table
              v-if="tableData.length != 0"
              id="table1"
              :data="tableData"
              border
              style="width: 100%"
            >
              <el-table-column
                v-for="(column, index) in tableColumns"
                :key="index"
                :prop="column.fields"
                :label="column.translate"
                :width="setWidth(column)"
                :align="'center'"
              >
                <template slot="header">
                  {{ column.translate }}
                  <el-tooltip
                    v-if="
                      column.fields == 'firstTimeoutRate' ||
                        column.fields == 'handleTimeoutRate' ||
                        column.fields == 'oneTimeResolutionRate' ||
                        column.fields == 'complaintRate' ||
                        column.fields == 'sheetCompletionRate' ||
                        column.fields == 'avgHandlePeriod'
                    "
                    class="item"
                    effect="dark"
                    :content="column.translate"
                  >
                    <i class="el-icon-question" />
                  </el-tooltip>
                </template>
                <template slot-scope="scope">
                  <span
                    v-if="
                      column.fields == 'firstTimeoutRate' ||
                        column.fields == 'handleTimeoutRate' ||
                        column.fields == 'oneTimeResolutionRate' ||
                        column.fields == 'sheetCompletionRate'
                    "
                    :class="
                      column.fields == 'firstTimeoutRate' ||
                        column.fields == 'handleTimeoutRate' ||
                        column.fields == 'oneTimeResolutionRate'
                        ? 'percent'
                        : ''
                    "
                  >
                    {{ parseFloat(scope.row[column.fields]).toFixed(2) + "%" }}
                  </span>
                  <span v-else>{{ scope.row[column.fields] }}</span>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
        <div class="block">
          <el-pagination
            :current-page="form.currentPage"
            :page-sizes="[2, 10, 50, 100, 200, 500, 1000]"
            :page-size="pageDate.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="sizeChange"
            @current-change="currentChange"
          />
        </div>
      </el-tabs>
      <div class="exportPane">
        <el-button
          :disabled="tableData.length == 0"
          icon="el-icon-upload2"
          @click="exportCenter"
        >导出中心</el-button>
        <el-button
          :disabled="tableData.length == 0"
          @click="exportTable"
        >导出</el-button>
      </div>
    </div>
  </div>
</template>
<script>
let that = ''
import { orderHandleAjax } from '@/api/customer.js'
import networkSelect from '@/components/menuComponent/NetworkSelect.vue'
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
import {
  getSheetChannelList,
  getsheetTypeList,
  getSolicitorList
} from '@/api/customer.js'
export default {
  components: {
    networkSelect
  },
  data() {
    return {
      form: {
        name: '',
        region: '',
        collectType: '', // 统计维度 1
        sheetChannelId: '', // 工单渠道 2
        sheetTypeId: '', // 工单类型 3
        processorId: '', // 员工id 4
        delivery: false,
        type: [],
        resource: '',
        currentPage: 1,
        value1: [],
        desc: ''
      },
      pageDate: {
        pageNum: 1,
        pageSize: 2
      },
      ul: {
        title: '投诉赔偿款',
        ticket: '29',
        num: '12'
      },
      ul2: {
        title: '投诉赔偿款',
        ticket: '29',
        num: '12'
      },
      tableColumns: [], // 表头
      tableData: [], // 数据
      activeType: '1', // 汇总类型()
      TypeDict: ['网点维度', '渠道维度', '类型维度', '处理人维度'],
      current: 1, // 当前页
      pages: 0, // 总页数
      total: 0, // 总条数
      sheetChannelList: [], // 工单渠道下拉
      sheetTypeList: [], // 工单类型下拉
      solicitorList: [] // 员工名称下拉
    }
  },
  created() {
    that = this
    that.getAjax()
    that.getList()
  },
  methods: {
    getList() {
      getSheetChannelList().then((res) => {
        if (res.code === 200) {
          console.log('下拉1', res.data)
          that.sheetChannelList = res.data
        }
      })
      getsheetTypeList().then((res) => {
        if (res.code === 200) {
          console.log('下拉2', res.data)
          that.sheetTypeList = res.data
        }
      })
      getSolicitorList().then((res) => {
        if (res.code === 200) {
          console.log('下拉3', res.data)
          this.solicitorList = res.data
        }
      })
    },
    handleClick(tab, event) {
      console.log(tab, event)
      that.activeType = tab.name
      // 选择某一维度,清空其他维度的查询条件
      that.form = {
        ...that.form,
        collectType: '', // 统计维度 1
        sheetChannelId: '', // 工单渠道 2
        sheetTypeId: '', // 工单类型 3
        processorId: '' // 员工id 4
      }
      that.getAjax()
    },
    handleSelect(val) {
      this.form.region = val
      console.log('select', val)
    },
    onSubmit() {
      that.pageDate.pageNum = 1
      that.getAjax()
    },
    resettingTap() {
      that.pageDate.pageNum = 1
      that.form = {
        name: '',
        region: '',
        collectType: '', // 统计维度 1
        sheetChannelId: '', // 工单渠道 2
        sheetTypeId: '', // 工单类型 3
        processorId: '', // 员工id 4
        delivery: false,
        type: [],
        resource: '',
        currentPage: 1,
        value1: [],
        desc: ''
      }
      that.getAjax()
    },
    sizeChange(e) {
      console.log(e)
      that.pageDate.pageSize = e
      that.getAjax()
    },
    currentChange(e) {
      console.log(e)
      that.pageDate.pageNum = e
      that.getAjax()
    },
    getAjax() {
      const data = {
        collectType: that.form.collectType, // 统计维度 1
        sheetChannelId: that.form.sheetChannelId, // 工单渠道 2
        sheetTypeId: that.form.sheetTypeId, // 工单类型 3
        processorId: that.form.processorId, // 员工id 4
        startTime: that.form.value1[0], // 开始时间
        endTime: that.form.value1[1], // 结束时间
        networkNameId: that.form.region,
        pageDate: that.pageDate,
        type: parseInt(that.activeType) // 汇总类型
      }
      // 去除无效字段
      for (const i in data) {
        if (!data[i]) delete data[i]
      }
      orderHandleAjax(data)
        .then((res) => {
          console.log(res, '工单处理质量统计数据')
          if (res.code === 200) {
            that.tableColumns = res.data.fields
            that.tableData = res.data.list.data
            that.current = res.data.list.current
            that.pages = res.data.list.pages
            that.total = res.data.list.total
          } else {
            that.reset()
          }
        })
        // eslint-disable-next-line handle-callback-err
        .catch((err) => {
          that.reset()
        })
    },
    reset() {
      // that.tableColumns = [];
      that.tableData = []
      that.current = 1
      that.pages = 0
      that.total = 0
    },
    setWidth(e) {
      return 80 + e.translate.length * 15
    },
    exportCenter() {},
    exportTable() {
      var xlsxParam = { raw: true }
      var wb = XLSX.utils.table_to_book(
        document.querySelector('#table1'),
        xlsxParam
      )
      var wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array'
      })
      const typeName = that.TypeDict[parseInt(that.activeType) - 1]
      try {
        FileSaver.saveAs(
          new Blob([wbout], { type: 'application/octet-stream' }),
          '工单处理质量统计-' + typeName + '.xlsx'
        )
      } catch (e) {
        if (typeof console !== 'undefined') console.log(e, wbout)
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.box {
  padding: 0px 20px;
}

.flex {
  display: flex;
  justify-content: flex-start;
  align-items: center;
  flex-wrap: wrap;
}
.liLittle {
  border: 1px solid #9999;
  height: 150px;
  display: flex;
  flex-flow: column;
  justify-content: space-around;
}
.liLittle > span {
  font-size: 24px;
}
.f-row {
  display: flex;
  flex-flow: row;
  justify-content: space-around;
}
.littbox {
  display: flex;
  flex-flow: column;
  justify-content: space-around;
  height: 100px;
}

.flex-bettom {
  display: flex;
  justify-content: space-between;
}

.header {
  padding: 20px 0;
  border-bottom: 1px solid #ccc;
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}
.span5 {
  min-width: 240px;
}
.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
.mytabs {
  position: relative;
}
.exportPane {
  position: absolute;
  top: 0;
  right: 0;
  height: 30px;
  display: flex;
  flex-flow: row nowrap;
  align-items: center;
  button {
    height: 100%;
    display: flex;
    flex-flow: row nowrap;
    justify-content: center;
    align-items: center;
  }
}
.percent {
  color: #409eff;
}
.block {
  display: flex;
  justify-content: flex-end;
}
</style>
